import Vue from 'vue'
import VueRouter from "vue-router";
import Account from '../layouts/account'
import Admin from '../layouts/admin'
import User from '../layouts/user'
import Goods from '../layouts/goods'
import store from '../store/index'
import utils from "../utils";
import Login from '../views/login.vue'
import Register from '../views/register.vue'
import Forget from '../views/forget.vue'
import GoodsCategory from '../views/goods/category.vue'
import GoodsBrand from '../views/goods/brand.vue'
import GoodsParams from '../views/goods/params.vue'
import UserManage from '../views/user/manage.vue'
import GoodsManage from '../views/goods/manage.vue'
import GoodsEdit from '../views/goods/edit.vue'
import UserInfo from '../views/user-info.vue'
import GoodsList from '../views/goods/list.vue'
import GoodsDetail from '../views/goods/detail.vue'
import Cart from '../views/cart/index.vue'
import OrderIndex from '../views/order/index.vue'
import OrderDetail from '../views/order/detail.vue'

Vue.use(VueRouter)

const routes = [{
  path: '/account',
  component: Account,
  children: [{
    path: '/login',
    component: Login
  }, {
    path: '/register',
    component: Register
  }, {
    path: '/forget',
    component: Forget
  }]
}, {
  path: '/admin',
  component: Admin,
  meta: {
    role: [1],
  },
  children: [{
    path: '/user/manage',
    component: UserManage,
    meta: {
      role: [1],
    },
  }, {
    path: '/goods',
    component: Goods,
    meta: {
      role: [1],
    },
    children: [{
      path: 'manage',
      component: GoodsManage,
      meta: {
        role: [1],
      },
    }]
  }, {
    path: '/goods/edit',
    component: GoodsEdit,
    meta: {
      role: [1],
    },
  }, {
    path: '/goods/category',
    component: GoodsCategory,
    meta: {
      role: [1],
    },
  }, {
    path: '/goods/brand',
    component: GoodsBrand,
    meta: {
      title: '商品品牌',
      role: [1]
    }
  }, {
    path: '/goods/params',
    component: GoodsParams,
    meta: {
      title: '参数设置',
      role: [1]
    }
  }, {
    path: 'info',
    component: UserInfo,
    meta: {
      role: [1]
    }
  }]
}, {
  path: '/user',
  component: User,
  meta: {
    role: 'user',
  },
  children: [{
    path: 'info',
    component: UserInfo,
    meta: {
      role: [2],
    },
  }, {
    path: '/goods',
    component: Goods,
    meta: {
      role: [2],
    },
    children: [{
      path: '/index',
      component: GoodsList,
      meta: {
        title: '商品列表',
        role: [2]
      }
    }, {
      path: 'detail',
      component: GoodsDetail,
      
      meta: {
        title: '商品详情',
        role: [2]
      }
    }]
  }, {
    path: '/cart',
    component: Cart,
    meta: {
      title: '购物车',
      role: [2]
    }
  }, {
    path: '/order',
    component: OrderIndex,
    
    meta: {
      title: '订单列表',
      role: [2]
    }
  }, {
    path: '/order/detail',
    component: OrderDetail,
    meta: {
      title: '订单详情',
      role: [2]
    }
  }]
}]

const router = new VueRouter({
  routes
})
router.beforeEach(async (to, from, next) => {
  let fullPath = to.fullPath,
    meta = to.meta || {},
    role = meta.role
  if (fullPath !== '/login' && fullPath !== '/register' && fullPath !== '/forget') {
    let isLogged = store.state.isLogged
    if (isLogged === undefined) {
      await store.dispatch('validateIsLogged')
    }
    let userInfo = store.state.userInfo
    if (!store.state.isLogged) {
      next({
        path: '/login'
      })
    } else {
      if (fullPath === '/') {
        if (userInfo.role === 1) {
          next('/user/manage')
        } else {
          next('/index')
        }
      } else {
        if (Array.isArray(role)) {
          if (role.includes(userInfo.role)) {
            next()
          } else {
            next('/')
          }
        } else {
          next()
        }
      }
    }
  } else {
    store.commit('updateUserInfo', {})
    next()
  }
})
router.afterEach((to, form) => {
  let meta = to.meta || {}
  utils.setTitle(meta.title)
})

export default router
